package com.ks.core.aop;


import com.ks.core.core.Result;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.exception.GunsExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局的的异常拦截器（拦截所有的控制器）（带有@RquestMapping注解的方法上都会拦截）
 *
 * @author kingdee
 * @date 2016年11月12日 下午3:19:56
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

 /*   *//**
     * 拦截jwt相关异常
     *//*
    @ExceptionHandler(JwtException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result jwtException(JwtException e) {
        log.error(BizExceptionEnum.TOKEN_ERROR.getMessage() + e.getMessage(), e);
        return Result.fail(BizExceptionEnum.TOKEN_ERROR);
    }

    *//**
     * 拦截jwt相关异常
     *//*
    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result jwtException(ExpiredJwtException e) {
        return Result.fail(BizExceptionEnum.TOKEN_EXPIRED);
    }*/

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result notFount(RuntimeException e) {
        e.printStackTrace();
        log.error(GunsExceptionEnum.SERVER_ERROR.getMessage() + "：" + e.getMessage(), e);
        return Result.fail(BizExceptionEnum.SERVER_ERROR);
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(GunsException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result notFount(GunsException e) {
        e.printStackTrace();
        log.error("GunsException:{},{}", e, e.getMessage());
        return Result.fail(e);
    }

    /**
     * 拦截空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result nullPointerException(NullPointerException e) {
        e.printStackTrace();
        log.error(GunsExceptionEnum.NULL_PARAM.getMessage() + e.getMessage(), e);
        return Result.fail(e.getMessage() + GunsExceptionEnum.NULL_PARAM.getMessage());
    }

    /**
     * 404异常
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result noHandlerFoundException(NoHandlerFoundException e) {
        log.error(GunsExceptionEnum.NOT_FOUND.getMessage(), e.getMessage());
        return Result.fail(GunsExceptionEnum.NOT_FOUND);
    }

}
